<template>
	<view class="safe-bottom">
		<view>
			<bg-search placeholder="输入人员姓名" @search="onSearch"></bg-search>
			<view class="m-3">
				<template v-if="dataList.length>0">
					<user-item :item="item" @click="checkItem(item)" v-for="item,idx in dataList" :key="idx"
						:checked="item.checked"></user-item>
				</template>
				<template v-else>
					<u-empty>
					</u-empty>
				</template>
			</view>
			<view class="bt-footer">
				<view class="flex align-center" style="width:100%">
					<view class="flex-1">
						<view>已选：{{selectedItem?selectedItem.nickName:''}}</view>
					</view>
					<view class="">
						<bg-btn @click="onConfirm" style="width: 300rpx;">确认</bg-btn>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import userItem from './userItem2.vue'
	import {
		usersList
	} from "@/api/system/user.js"
	export default {
		components: {
			userItem
		},
		onLoad(options) {
			this.work = options.work
			this.params.projectId = options.pid
		},
		mounted() {
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('acceptDataFromOpener', (res) => {
				this.obj = res.data
			})
			this.getData()
		},
		data() {
			return {
				work: '',
				params: {
					projectId: '',
					realName: '',
					pageNum: 1,
					pageSize: 20
				},
				dataList: [],
				selectedItem: null
			}
		},
		methods: {
			onSearch(e) {
				this.params.realName = e
				this.params.pageNum = 1
				this.dataList = []
				this.getData()
			},
			getData() {
				if (this.work == 'safetyManager' || this.work == 'financeManager') {
					let work = this.work
					this.params.role = this.work == 'safetyManager' ? 'safety' : 'fanance'
				}
				usersList(this.params).then(res => {
					res.data.rows.forEach(item => {
						this.$set(item, "checked", false)
					})
					this.dataList = [...this.dataList, ...res.data.rows]
				})
			},
			checkItem(item) {
				this.dataList.forEach(_item => {
					this.$set(_item, "checked", false)
				})
				item.checked = true
				this.selectedItem = item
			},
			onConfirm() {
				if (this.selectedItem) {
					if (this.work) {
						let returnObj = {}
						returnObj[this.work + "UserId"] = this.selectedItem.userId
						returnObj[this.work + "UserName"] = this.selectedItem.nickName
						uni.$emit("selected-user-work", returnObj)
					} else {
						uni.$emit("selected-user", this.selectedItem)
					}
					uni.navigateBack()
				} else {
					this.$modal.msgError("请选择用户")
				}
			}
		}
	}
</script>

<style>

</style>